﻿Imports System.Xml
Imports System.Data.OleDb

Partial Public Class getTagCount
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim xmlDoc As New XmlDocument()
        Dim rootNode As XmlNode = xmlDoc.CreateElement("TagCount")
        xmlDoc.AppendChild(rootNode)

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("mobilestandardsdb").ConnectionString)
        oleDbConn.Open()
        Dim tagCountSql As String = "SELECT papersweeksandtags.Tag, Count(*) AS TagCount FROM papersweeksandtags GROUP BY papersweeksandtags.Tag"
        Dim tagCountCmd As New OleDbCommand(tagCountSql, oleDbConn)
        Dim tagCountReader = tagCountCmd.ExecuteReader()
        If tagCountReader.HasRows() Then
            While tagCountReader.Read()
                Dim tmpTagCountNode As XmlElement = xmlDoc.CreateElement("Tag")
                tmpTagCountNode.SetAttribute("Name", tagCountReader("Tag"))
                tmpTagCountNode.SetAttribute("Count", tagCountReader("TagCount"))
                rootNode.AppendChild(tmpTagCountNode)
            End While
        End If
        oleDbConn.Close()
        Dim sw As New System.IO.StringWriter()
        Dim xw As New XmlTextWriter(sw)
        xmlDoc.WriteTo(xw)
        Response.Write(sw.ToString())
    End Sub


End Class